<?php
/**
 * Description of ArticleClass
 *
 * @version  1.0 $Id: article.php 13 2012-07-29 18:25:25Z webbochsant@gmail.com $
 * @author Daniel Eliasson (joomla at stilero.com)
 * @copyright  (C) 2012-feb-5 Stilero Webdesign http://www.stilero.com
 * @license	GPLv2
 * 
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * 
 * This file is part of AutoSocialShare.
 * 
 * AutoSocialShare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AutoSocialShare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AutoSocialShare.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
class Article {
    private $article;
    private $type;
    private $zooparams;
    var $id;
    var $introtext;
    var $fulltext;
    var $language;
    var $link;
    var $full_url;
    var $tags;
    var $title;
    var $catid;
    var $access;
    var $publish_up;
    var $published;
    const JOOMLA = 1;
    const K2 = 2;
    const ZOO = 3;
    
    /**
     *  Main constructor
     * @param object    $article    Joomla-article / K2-item / ZooEvent
     * @param string    $type    joomla/k2/zoo
     */
    public function __construct($article, $type="") {
        $this->article = $article;
        if( $type == 'k2' ){
            $this->type = self::K2;
        }else if ( $type == 'zoo'){
            $this->type = self::ZOO;
        }else{
            $this->type = self::JOOMLA;
        }
        if( $this->type == self::ZOO ){
            $this->article = $article->getSubject();
            $this->zooparams = $article->getParameters();
            $this->title = $this->article->name;
            $this->catid = $this->article->params->get('config.primary_category');
            $this->published = $this->article->state;
        }else{
            $this->title = isset($this->article->title) ? $this->article->title : '';
            $this->catid = ( isset( $this->article->catid ) ) ? $this->article->catid : 0;
            $this->published = (isset($this->article->published)) ? $this->article->published : 0;
            $this->introtext = isset($this->article->introtext)?$this->article->introtext:'';
            $this->fulltext = isset($this->article->fulltext) ? $this->article->fulltext : '';
        }
        
        $this->id = isset($this->article->id) ? $this->article->id : 0;
        $this->language = (isset($this->article->language)) ? $this->article->language : "";
        $this->link = isset($this->article->alias) ? $this->article->alias : '';
        $this->full_url = isset($this->article->id) ? $this->getFullURL() : '';
        $this->tags = is_object($this->article) ? $this->getTagsArray() : '';
        $this->access = isset($this->article->access) ? $this->article->access : 0;
        $this->publish_up = isset($this->article->publish_up) ? $this->article->publish_up : '0000-00-00 00:00:00';
    }
    
    public function getFullURL() {
        $urlQuery = "";
        switch ($this->type) {
            case self::JOOMLA:
                $urlQuery = "?option=com_content&view=article&id=".$this->id;
                break;
            case self::K2:
                $urlQuery = "?option=com_k2&view=item&id=".$this->id;
                break;
            case self::ZOO:
                 $urlQuery = "?option=com_zoo&task=item&item_id=".$this->id;
                break;
            default:
                break;
        }
        $fullURL = JURI::root()."index.php".$urlQuery;
        return $fullURL;
    }
    
    public function getTagsArray(){
        $tags = array();
        if( $this->type == self::JOOMLA ){
            $commaSepTags = $this->article->metakey;
            $metaKeyArray = explode(",", $commaSpearatedMetaKeys);
            foreach ($metaKeyArray as $key => $value) {
                $tags[] = trim(str_replace(" ", "", $value));
            }
        }else if($this->type == self::K2){
            $tags = $this->queryK2TagsArray();
        }else if( $this->type == self::ZOO ){
            $tags = $this->article->getTags();
        }
        return $tags;
    }
    
    private function queryK2TagsArray() {
        $query;
        $db = &JFactory::getDbo();
        $query = "SELECT ".$db->nameQuote('name').
            " FROM ".$db->nameQuote('#__k2_tags')." AS t".
            " INNER JOIN " . $db->nameQuote('#__k2_tags_xref')." AS x".
            " ON  x.tagID = t.id".
            " WHERE x.itemID = " . $db->Quote($this->id);
        $db->setQuery($query);
        $k2ItemTags = $db->loadResultArray ();
        return $k2ItemTags;
    }
    
}

?>
